package modes

import (
	"fmt"
)

/*
 * 描述：订单--网课
 *
 ********************************************************************************************/
type OrderClasses struct {
	Id        int64  `json:"cla_id" xorm:"id" form:"-"`                                   // 订单ID
	PId       int64  `json:"cla_pid" xorm:"pid" form:"pid"`                               // 项目ID
	PName     string `json:"cla_pname" xorm:"pname" form:"pname"`                         // 项目名称
	CId       int64  `json:"cla_cid" xorm:"cid" form:"cid" binding:"required"`            // 班型ID
	CName     string `json:"cla_cname" xorm:"cname" form:"cname" binding:"required"`      // 班型名称
	OrderId   int64  `json:"cla_order_id" xorm:"order_id" form:"oid" binding:"required"`  // 订单ID
	RealPrice int64  `json:"sig_price" xorm:"real_price" form:"price" binding:"required"` // 实付价格

	IsAgree int `json:"is_agree" xorm:"-" form:"-"` // 是否有协议 1有 2没有

}

func (this *OrderClasses) TableName() string {
	return "order_classes"
}

func (this *OrderClasses) Save() (int64, error) {
	return Db(2).Insert(this)
}

func (this *OrderClasses) Get() (bool, error) {
	return Db(2).Get(this)
}
func (this *OrderClasses) Where(fage, count, page int, where, field string) ([]OrderClasses, error) {
	list := make([]OrderClasses, 0)
	var err error
	if field == "" {
		field = "id"
	}
	page--
	if 0 == fage { // 从小到大排序
		err = Db(2).Table(this.TableName()).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(2).Table(this.TableName()).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

func (this *OrderClasses) List(id int64) ([]OrderClasses, error) {
	where := fmt.Sprintf("order_id = %d", id)
	return this.Where(1, 10, 0, where, "")
}
func (this *OrderClasses) GetListByOrderId(cidList string) ([]OrderClasses, error) {
	where := fmt.Sprintf("order_id = %d and cid in(%s) ", this.OrderId, cidList)
	list := make([]OrderClasses, 0)
	err := Db(2).Table(this.TableName()).Where(where).
		Find(&list)
	return list, err
}

func (this *OrderClasses) ListByIds(ids []int64) ([]OrderClasses, error) {
	list := make([]OrderClasses, 0)
	err := Db(2).Table(this.TableName()).In("order_id", ids).Find(&list)
	return list, err
}
